package com.white.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.white.bean.Classify;
import com.white.bean.UserModel;
import com.white.service.ClassifyService;
import com.white.service.UserModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/userModel")
public class UserModelController {
    @Autowired
    UserModelService userModelService;

    @Autowired
    ClassifyService classifyService;


    /**
     * 增加用户浏览时长
     * @param seconds
     */
    @RequestMapping("/incViewDuration/{articleId}/{seconds}")
    @ResponseBody
    public void incViewDuration(@CookieValue(UserController.LOGIN_USER_ID) Long userId,
                                @PathVariable("articleId") Long articleId,
                                @PathVariable("seconds") int seconds) {
        userModelService.incViewDuration(userId, articleId, seconds);
    }


    @RequestMapping("/searchByUserId/{userId}")
    @ResponseBody
    public List<Object> searchByUserId(@PathVariable("userId") Long userId) {
        List<Object> ans = new ArrayList<>();
        QueryWrapper<UserModel> query = new QueryWrapper<>();
        query.eq("userId", userId);
        List<UserModel> userModels = userModelService.searchById(userId);
        for (UserModel userModel : userModels) {
            Map<String, Object> map = new HashMap<>();
            map.put("userModel", userModel);
            /*获取对应的分类*/
            Classify classify = classifyService.getById(userModel.getClassifyId());
            map.put("classify", classify);
            ans.add(map);
        }
        return ans;
    }
}
